package vn.fpt.snake.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.List;

import vn.fpt.snake.commons.Constants;
import vn.fpt.snake.controller.GameControllerImpl;
import vn.fpt.snake.entity.Settings;
import vn.fpt.snake.view.Board;

public class InstructionViewImpl implements IView {
	Board parent;
	GameControllerImpl gameController;
	List<String> instructions;
	File file;
	Settings settings;
	
	public InstructionViewImpl(Board parent) throws Exception{
		this.parent = parent;
		gameController = new GameControllerImpl();
		file = gameController.getFile(Constants.INSTRUCTION_FILE);
		instructions = gameController.getTextFromFile(file);
		settings = gameController.getSettings();
	}
	@Override
	public void onPaint(Graphics g) throws Exception{
		// TODO Auto-generated method stub
		int size = instructions.size();
		int width = parent.getWidth();
		int height = parent.getHeight();
		int dotSize = settings.getDotSize();
		Font font = new Font("Cooper Black", Font.BOLD, (int)(dotSize * 1.5));
		FontMetrics metrics = g.getFontMetrics(font);
		String instructionText = Constants.INSTRUCTION;
		
		int instructionWidth = metrics.stringWidth(instructionText);
		int heightOfFont = metrics.getHeight();
		int xInstruction = (width - instructionWidth) / 2;
		int yInstruction = (height / 3) - heightOfFont;
		
		g.drawImage(gameController.getImage(Constants.GAME_OVER_IMG), 0, 0, parent.width, parent.height, parent);
		g.setColor(Color.WHITE);
		g.drawString(Constants.BACK_INSTRUCTION, dotSize, dotSize);
		g.setFont(font);
		g.setColor(new Color(213, 124, 54));
		g.drawString(instructionText, xInstruction, yInstruction);
		
		for(int i = 0; i < size; i++){
			String instruction = instructions.get(i);
				
			font = new Font("Cooper Black", Font.BOLD, dotSize);
			metrics = g.getFontMetrics(font);
			
			int xText = width / 3;
			int yText = yInstruction + heightOfFont + metrics.getHeight() * (i + 2);

			g.setColor(Color.BLUE);
			g.drawString(instruction, xText, yText);
		
		}				
	}
	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		try{
			int keyCode = e.getKeyCode();
			if(parent._view instanceof InstructionViewImpl){
				if(keyCode == KeyEvent.VK_ESCAPE){					
					gameController.switchStartupView(parent);
				}
			}
		}catch (Exception ex) {
			// TODO: handle exception
			ex.printStackTrace();
		}
	}
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

}
